let fileExtensions = [];
let globaldentify = '';
dataOptions=[{text : 'Landsat8',id : 'Landsat8 '},{text : '资源三号',id : '资源三号 '}];
$(function () {
    //初始化表单选项
    fromInit();
    //注册时间
    formEvent();
});

function fromInit() {
    /**
     * 上传图片
     */
    $("#file").fileinput({
        language: 'zh',
        uploadUrl: basePath + "/picture/uploadMulti",
        dataType: 'text/json,charset=utf-8',
        browseClass: "btn btn-primary", //按钮样式
        allowedFileExtensions: fileExtensions,
        uploadAsync: false,//同步
        overwriteInitial: false,
        showCancel: false,
        maxFileSize: 1024 * 1000,
        maxFileCount: 5,
        elem: '#file', // 绑定上传按钮
        auto: false, // 关闭自动上传
        layoutTemplates: {
            actionUpload: ''
        },
        uploadExtraData: function (previewId, index) {   //额外参数的关键点
            let obj = {
                'allmodel': globaldentify
            };
            return obj;
        },
        choose: function(obj){ // 选择文件后的回调函数
            // 设置打开指定盘符位置
            var url = "D:\\毕设资料\\text\\"; // 可以根据实际需要修改盘符和目录
            var iframe = document.createElement("iframe");
            iframe.style.display = "none";
            document.body.appendChild(iframe);
            iframe.src = url;
        }
    }).on("filebatchuploadsuccess", function (e, data) { //异步上传返回结果处理
        var result = data.response;
        if (result.code !== null && result.code === 'ok') {
            let fileids = result.data;
            //上传成功
            layer.msg('检测成功!', {icon: 1, time: 1500}, function () {
                $('#file').fileinput('clear');
                layer.closeAll();
            });
        } else {
            layer.msg('图片检测失败！', {icon: 2, time: 1500}, function () {
                $('#file').fileinput('clear');
                layer.closeAll();
            });
        }
    }).on('fileerror', function (event, data, msg) {
        layer.msg('图片检测失败！', {icon: 2, time: 1500}, function () {
            $('#file').fileinput('clear');
            layer.closeAll();
        });
    }).on("filebatchselected", function (event, files) {

    });

    $("#dataList").select2({
        placeholder: "数据集",
        data: dataOptions,
        allowClear: true
    });

    $.hcPostAjax({
        msgPrefix: '模型加载失败',
        url: basePath + '/model/newlist',
        success: function (data) {
            var modelData = [];
            $.each(data.list, function (i) {
                var option = {id: data.list[i].name + "", text: data.list[i].name};
                modelData.push(option);
            })
            $("#allmodel").select2({
                placeholder: "模型名称",
                data: modelData,
                allowClear: true
            });

        }
    });
}

function save() {
    let params = getFormData('.layui-form');
    //提交
    params.applyStatus = '00';
    $.hcPostAjax({
        showLoad: true,
        msgPrefix: '提交出错:',
        url: `${basePath}/picture/realAdd`,
        data: params,
        success: function (data) {
            $("#file").fileinput("upload");
        }
    });

}

function formEvent() {
    /**
     * 保存
     */
    $("#edit_yes").on("click", function () {
        form.on('submit(save_form)', function (data) {
            if (isEmpty($('#file').val())) {
                return alertMsg('请先上传文件');
            }
            save();
        });
    })
    /**
     * 取消修改
     */
    $("#edit_cancle").on("click", function () {
        parent.layer.closeAll();
    });
}
